package com.ow;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.osmdroid.DefaultResourceProxyImpl;
import org.osmdroid.ResourceProxy;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedIconOverlay;
import org.osmdroid.views.overlay.ItemizedOverlay;
import org.osmdroid.views.overlay.OverlayItem;
import org.osmdroid.views.overlay.ScaleBarOverlay;

import java.util.ArrayList;


public class OSMap extends Activity {
    private MapView mapView;
    private MapController mapController;
    private LocationManager locationManager;

    private ItemizedOverlay<OverlayItem> myMarkerOverlay;

    private ArrayList<OverlayItem> myMarkerArray;

    private ResourceProxy resourceProxy;
    private GeoPoint centerPoint;

    Button centerButton;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.osmap);

        resourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Location lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setTileSource(TileSourceFactory.MAPNIK);
        mapView.setBuiltInZoomControls(true);
        mapView.setMultiTouchControls(true);


        mapController = mapView.getController();
        mapController.setZoom(13);

        myMarkerArray = new ArrayList<OverlayItem>();
        ArrayList<OverlayItem> educationArray = new ArrayList<OverlayItem>();
        ArrayList<OverlayItem> foodArray = new ArrayList<OverlayItem>();
        ArrayList<OverlayItem> hotelArray = new ArrayList<OverlayItem>();
        ArrayList<OverlayItem> bankArray = new ArrayList<OverlayItem>();


        if (lastLocation != null) {
            updateLoc(lastLocation);
        }

        ScaleBarOverlay myScaleBarOverlay = new ScaleBarOverlay(this);
        this.mapView.getOverlays().add(myScaleBarOverlay);

        addListenerOnButton();

        /*------------------------------------------------------------------------------------------------------------*/

        OverlayItem mephi = new OverlayItem("НИЯУ МИФИ, <адрес>", "Что-то невероятно интересное", new GeoPoint((int) (55.649884 * 1E6), (int) (37.664341 * 1E6)));
        OverlayItem msu = new OverlayItem("МГУ, <адрес>", "Что-то невероятно интересное", new GeoPoint((int) (55.70292 * 1E6), (int) (37.530703 * 1E6)));
        OverlayItem sharaga = new OverlayItem("Какая-то шарага, <адрес>", "Что-то невероятно интересное", new GeoPoint((int) (55.633907 * 1E6), (int) (37.652367 * 1E6)));
        Drawable educationMarker = this.getResources().getDrawable(R.drawable.university);
        mephi.setMarker(educationMarker);
        msu.setMarker(educationMarker);
        sharaga.setMarker(educationMarker);
        educationArray.add(mephi);
        educationArray.add(msu);
        educationArray.add(sharaga);
        ItemizedOverlay<OverlayItem> educationOverlay = new ItemizedIconOverlay<OverlayItem>(educationArray,
                new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
                    @Override
                    public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                        Toast.makeText(OSMap.this, item.mTitle, Toast.LENGTH_LONG).show();
                        return true;
                    }

                    @Override
                    public boolean onItemLongPress(final int index, final OverlayItem item) {
                        Toast.makeText(OSMap.this, item.mDescription, Toast.LENGTH_LONG).show();
                        return true;
                    }
                }, resourceProxy);
        this.mapView.getOverlays().add(educationOverlay);


        OverlayItem kfc = new OverlayItem("KFC, <адрес>", "Что-то невероятно интересное", new GeoPoint((int) (55.648801 * 1E6), (int) (37.656326 * 1E6)));
        OverlayItem macdonalds = new OverlayItem("Макдоналдс, <адрес>", "Что-то невероятно интересное", new GeoPoint((int) (55.667589 * 1E6), (int) (37.658418 * 1E6)));
        OverlayItem shaurma = new OverlayItem("Шаурма у Ашота, <адрес>", "Что-то невероятно интересное", new GeoPoint((int) (55.655092 * 1E6), (int) (37.649868 * 1E6)));
        Drawable foodMarker = this.getResources().getDrawable(R.drawable.food);
        kfc.setMarker(foodMarker);
        macdonalds.setMarker(foodMarker);
        shaurma.setMarker(foodMarker);
        foodArray.add(kfc);
        foodArray.add(macdonalds);
        foodArray.add(shaurma);
        ItemizedOverlay<OverlayItem> foodOverlay = new ItemizedIconOverlay<OverlayItem>(foodArray,
                new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
                    @Override
                    public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                        Toast.makeText(OSMap.this, item.mTitle, Toast.LENGTH_LONG).show();
                        return true;
                    }

                    @Override
                    public boolean onItemLongPress(final int index, final OverlayItem item) {
                        Toast.makeText(OSMap.this, item.mDescription, Toast.LENGTH_LONG).show();
                        return true;
                    }
                }, resourceProxy);
        this.mapView.getOverlays().add(foodOverlay);


        OverlayItem milan = new OverlayItem("Отель 'Милан', <адрес>", "Что-то невероятно интересное", new GeoPoint((int) (55.618798 * 1E6), (int) (37.715099 * 1E6)));
        Drawable hotelMarker = this.getResources().getDrawable(R.drawable.hotel);
        milan.setMarker(hotelMarker);
        hotelArray.add(milan);
        ItemizedOverlay<OverlayItem> hotelOverlay = new ItemizedIconOverlay<OverlayItem>(hotelArray,
                new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
                    @Override
                    public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                        Toast.makeText(OSMap.this, item.mTitle, Toast.LENGTH_LONG).show();
                        return true;
                    }

                    @Override
                    public boolean onItemLongPress(final int index, final OverlayItem item) {
                        Toast.makeText(OSMap.this, item.mDescription, Toast.LENGTH_LONG).show();
                        return true;
                    }
                }, resourceProxy);
        this.mapView.getOverlays().add(hotelOverlay);


        OverlayItem sberbank = new OverlayItem("Сбербанк, <адрес>", "Что-то невероятно интересное", new GeoPoint((int) (55.640427 * 1E6), (int) (37.662802 * 1E6)));
        OverlayItem avangard = new OverlayItem("Банк 'Авангард', <адрес>", "Что-то невероятно интересное", new GeoPoint((int) (55.641229 * 1E6), (int) (37.656173 * 1E6)));
        Drawable bankMarker = this.getResources().getDrawable(R.drawable.bank);
        sberbank.setMarker(bankMarker);
        avangard.setMarker(bankMarker);
        bankArray.add(sberbank);
        bankArray.add(avangard);
        ItemizedOverlay<OverlayItem> bankOverlay = new ItemizedIconOverlay<OverlayItem>(bankArray,
                new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
                    @Override
                    public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                        Toast.makeText(OSMap.this, item.mTitle, Toast.LENGTH_LONG).show();
                        return true;
                    }

                    @Override
                    public boolean onItemLongPress(final int index, final OverlayItem item) {
                        Toast.makeText(OSMap.this, item.mDescription, Toast.LENGTH_LONG).show();
                        return true;
                    }
                }, resourceProxy);
        this.mapView.getOverlays().add(bankOverlay);

    }

    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, myLocationListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(myLocationListener);
    }

    private void updateLoc(Location loc) {
        GeoPoint locGeoPoint = new GeoPoint(loc.getLatitude(), loc.getLongitude());
        mapController.setCenter(locGeoPoint);
        setOverlayLoc(loc);
        mapView.invalidate();
    }

    private void setOverlayLoc(Location overlayloc) {
        centerPoint = new GeoPoint(overlayloc);

        myMarkerArray.clear();
        this.mapView.getOverlays().remove(this.myMarkerOverlay);

        OverlayItem item = new OverlayItem("Вы тут!", "Точно тут!", centerPoint);
        Drawable marker = this.getResources().getDrawable(R.drawable.marker);
        item.setMarker(marker);
        myMarkerArray.add(item);

        this.myMarkerOverlay = new ItemizedIconOverlay<OverlayItem>(myMarkerArray,
                new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
                    @Override
                    public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                        Toast.makeText(OSMap.this, item.mTitle, Toast.LENGTH_LONG).show();
                        return true;
                    }

                    @Override
                    public boolean onItemLongPress(final int index, final OverlayItem item) {
                        Toast.makeText(OSMap.this, item.mDescription, Toast.LENGTH_LONG).show();
                        return true;
                    }
                }, resourceProxy);
        this.mapView.getOverlays().add(this.myMarkerOverlay);
    }

    private LocationListener myLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            updateLoc(location);
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

    };


    public void addListenerOnButton() {
        centerButton = (Button) findViewById(R.id.centerButton);
        centerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mapController.animateTo(centerPoint);
                //mapController.setCenter(geoPoint);
            }
        });
    }
}

